package com.atguigu.srb.common.exception;

import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.result.ResponseResult;
import com.fasterxml.jackson.core.JsonParseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author Miao
 * @version 1.0.0
 * @date 2022年09月30日 17:26:25
 * @packageName com.atguigu.srb.common.exception
 * @className UnifiedExceptionHandle
 * @describe 统一异常处理
 */

@RestControllerAdvice
@Slf4j
public class UnifiedExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    public ResponseResult handleException(Exception e){
        log.error(e.getMessage(),e);
        return ResponseResult.result(ResponseEnum.ERROR);
    }

    @ExceptionHandler(value = JsonParseException.class)
    public ResponseResult handleException(JsonParseException e){
        log.error(e.getMessage(),e);
        return ResponseResult.result(ResponseEnum.JSON_PARSE_ERROR);
    }

    @ExceptionHandler(value = BadSqlGrammarException.class)
    public ResponseResult handleException(BadSqlGrammarException e){
        log.error(e.getMessage(),e);
        return ResponseResult.result(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }

    @ExceptionHandler(value = BusinessException.class )
    public ResponseResult handleException(BusinessException e){
        log.error(e.getMessage(),e);
        return ResponseResult.result(e.getCode(),e.getMessage());
    }

    /**
     * Controller上一层相关异常
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public ResponseResult handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return ResponseResult.result(ResponseEnum.SERVLET_ERROR.getCode(),ResponseEnum.SERVLET_ERROR.getMessage());
    }
}
